import java.util.ArrayList;
import java.util.List;

public class Area {
	String areaName, areaType, areaID;
	Room currentLocation;
	List<Room> roomArray = new ArrayList<Room>();
	
	public Area(String name, String type, String ID){
		areaName = name;
		areaType = type;
		areaID = ID;
	}
	
	public void buildArea(List<Room> areaRooms){
			
		// Exit order N, S, E, W, U, D
		
		roomArray.add(new Room("FIGARO0001","Main Street - Central","You are standing in central main street",new Exit("FIGARO0002",'n'),new Exit("FIGARO0004",'s'),new Exit("FIGARO0003",'e'),new Exit("FIGARO0005",'w'),null,null));
		roomArray.add(new Room("FIGARO0002","Main Street - North","You are standing on north main street",new Exit("FIGARO0006",'n',"Door"),new Exit("FIGARO0001",'s'),null,null,null,null));
		roomArray.add(new Room("FIGARO0003","Main Street - East","You are standing on east main street",null,null,null,new Exit("FIGARO0001",'w'),null,null));
		roomArray.add(new Room("FIGARO0004","Main Street - South","You are standing on south main street",new Exit("FIGARO0001",'n'),null,null,null,null,null));
		roomArray.add(new Room("FIGARO0005","Main Street - West","You are standing on west main street",null,null,new Exit("FIGARO0001",'e'),null,null,null));
		roomArray.add(new Room("FIGARO0006","Armor Shop","You are standing in the northern armor shop",null,new Exit("FIGARO0002",'s',"Door"),null,null,null,null));
	}
	
	public void enterArea(char direction){
		currentLocation = getRoomByID("FIGARO0001");
	}
	
	public String moveCharacter(char direction){
		
		//Check for mount
		//if(player.state != impaired)
		//Check if stamina/health good enough
			//Sub stamina if necessary when moving/crawling/etc
		
		Exit tempExit = currentLocation.getExit(direction);
		
		if(tempExit != null){
			if(!tempExit.doorState){
				currentLocation = getRoomByID(tempExit.pointer);
				
				return "You move " + getFullDirection(direction)  + "\n" + currentLocation;
			}
			else{
				return "The door is closed";
			}
		}
		else{
			return "There is no exit that direction!";
		}
	}
	
	public Room getRoomByID(String ID){
		Room returnRoom = null;
		
		for(int i=0;i<roomArray.size();i++){
			if(roomArray.get(i).roomID == ID){
				returnRoom = roomArray.get(i);
			}
		}
		
		return returnRoom;
	}
	
	public String showExits(){
		char[] exitChars = currentLocation.exitList().toCharArray();
		String temp = "";
		
		for(int i=0;i<exitChars.length;i++){
			if(i!= (exitChars.length-1)){
				if(exitChars[i] != ',')
					temp = temp + getFullDirection(exitChars[i]) + ",";
			}	
			else
				temp = temp + getFullDirection(exitChars[i]);	
		}
		
		return "Exits: " + temp;
	}
	
	public String showExitsDebug(){
		char[] exitChars = currentLocation.exitList().toCharArray();
		String temp = "";
		
		for(int i=0;i<exitChars.length;i++){
			if(i!= (exitChars.length-1)){
				if(exitChars[i] != ',')
					temp = temp + getFullDirection(exitChars[i]) + ": " + currentLocation.getExit(exitChars[i]).pointer + "\n";
			}	
			else
				temp = temp + getFullDirection(exitChars[i]) + ": " + currentLocation.getExit(exitChars[i]).pointer;	
		}
		
		return "Exits: " + temp;
	}
	
	public void open(String command){
		boolean flag = false;
		String object = command.substring(5,command.length());
		
		if(command.length() == 4){
			System.out.println("Open what?");
			flag = true;
		}	
		
		if(!flag){
			char direction = object.trim().toLowerCase().charAt(0); 
			System.out.println(openExitDoor(direction));				
		}
	}	
	
	public void close(String command){
		boolean flag = false;
		String object = command.substring(5,command.length());
		
		if(command.length() == 4){
			System.out.println("Close what?");
			flag = true;
		}	
		
		if(!flag){
			char direction = object.trim().toLowerCase().charAt(0); 
			System.out.println(closeExitDoor(direction));				
		}
	}	
	
	public String openExitDoor(char direction){
		String returnString = "";
		Room reverseRoom = getRoomByID(currentLocation.getExit(direction).pointer);
		
		//Check for door itself
		if(!(currentLocation.getExit(direction).door) && !(currentLocation.getExit(direction).hidden)){
			returnString = "There is no door in that direction";
		}
		else{
			//Check if already open
			if(!(currentLocation.getExit(direction).doorState)){
				returnString = "The door is already open";
			}
			else{
				//Check if locked
				if(currentLocation.getExit(direction).locked){
					returnString = "The door is locked";
				}
				else{
					//SKILLCHECK
					currentLocation.getExit(direction).toggleDoorState();
					reverseRoom.getExit(getOppositeDirection(direction)).toggleDoorState();
					returnString = "You open the door to the " + getFullDirection(direction);
				}
			}
		}
		return returnString;
	}
	
	public String closeExitDoor(char direction){
		String returnString = "";
		Room reverseRoom = getRoomByID(currentLocation.getExit(direction).pointer);
		
		//Check for door itself
		if(!(currentLocation.getExit(direction).door) && !(currentLocation.getExit(direction).hidden)){
			returnString = "There is no door in that direction";
		}
		else{
			//Check if already open
			if(currentLocation.getExit(direction).doorState){
				returnString = "The door is already closed";
			}
			else{
				currentLocation.getExit(direction).toggleDoorState();
				reverseRoom.getExit(getOppositeDirection(direction)).toggleDoorState();
				returnString = "You close the door to the " + getFullDirection(direction);
			}
		}
		return returnString;
	}
	
	public String getFullDirection(char direction){
		if(direction=='n'){
			return "north";
		}
		else if(direction=='s'){
			return "south";
		}
		else if(direction=='e'){
			return "east";
		}
		else if(direction=='w'){
			return "west";
		}
		else if(direction=='u'){
			return "up";
		}
		else{
			return "down";
		}
	}
	
	public char getOppositeDirection(char exit){
		if(exit == 'n')
			return 's';
		else if(exit == 's')
			return 'n';
		else if(exit == 'w')
			return 'e';
		else if(exit == 'e')
			return 'w';
		else if(exit == 'u')
			return 'd';
		else return 'u';
	}
}
